import cv2
import numpy as np

img = cv2.imread('img/person.jpg')
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# cv2.imshow('', img)
lowerb = np.array([78, 43, 46])
upperb = np.array([106, 255, 255])
blue_mask = cv2.inRange(img_hsv, lowerb, upperb)

# 开闭运算配合使用，否则边缘要走位
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
blue_mask = cv2.morphologyEx(blue_mask, cv2.MORPH_CLOSE, kernel)
blue_mask = cv2.morphologyEx(blue_mask, cv2.MORPH_OPEN, kernel)
blue_mask = cv2.morphologyEx(blue_mask, cv2.MORPH_CLOSE, kernel)
blue_mask = cv2.morphologyEx(blue_mask, cv2.MORPH_DILATE, kernel)

h, w, c = img.shape
tmp = img.copy()
for i in range(h):
    for j in range(w):
        if blue_mask[i,j] == 255:
            tmp[i,j] = (0, 0, 255)
cv2.imshow('', tmp)
cv2.waitKey(0)